package com.china08.yxyapi.api.home.kecheng.topic;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicCatalog;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;

/**
 * 专题课程--列表页--一级分类名称
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengTopicOneController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsTopicCatalogRepository yxyLessonsTopicCatalogRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/one/phace/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request,
            HttpServletResponse response) {

        List<RespModel> returnLst = new ArrayList<>();
        RespModel aa = new RespModel();
        aa.idOne = "";
        aa.nameOne = "不限";
        aa.idTwo = "";
        aa.nameTwo = "不限";
        aa.idThree = "";
        aa.nameThree = "不限";
        returnLst.add(aa);
        // 一级栏目
        List<YxyLessonsTopicCatalog> yxyLessonsTopicCatalogLst1 = yxyLessonsTopicCatalogRepository
                .findByLevelOrderByOrderNumAsc(1);

        if (yxyLessonsTopicCatalogLst1 != null) {
            for (YxyLessonsTopicCatalog yxyLessonsTopicCatalog : yxyLessonsTopicCatalogLst1) {
                RespModel model = new RespModel();
                List<CatalogTwo> catalogTwoList = new ArrayList<CatalogTwo>();
                model.idOne = yxyLessonsTopicCatalog.getId();
                model.nameOne = yxyLessonsTopicCatalog.getName();

                String id1 = yxyLessonsTopicCatalog.getId();

                // 二級
                List<YxyLessonsTopicCatalog> yxyLessonsTopicCatalogLst = yxyLessonsTopicCatalogRepository
                        .findByPidAndLevel(id1, 2);

                if (yxyLessonsTopicCatalogLst != null) {
                    for (YxyLessonsTopicCatalog catalog2 : yxyLessonsTopicCatalogLst) {
                        CatalogTwo catalogTwo = new CatalogTwo();
                        catalogTwo.idTwo = catalog2.getId();
                        catalogTwo.nameTwo = catalog2.getName();

                        // 三級
                        List<CatalogThree> catalogThreeList = new ArrayList<>();
                        String id2 = catalog2.getId();

                        List<YxyLessonsTopicCatalog> yxyLessonsTopicCatalogLst3 = yxyLessonsTopicCatalogRepository
                                .findByPidAndLevel(id2, 3);
                        if (yxyLessonsTopicCatalogLst3 != null) {
                            for (YxyLessonsTopicCatalog catalog3 : yxyLessonsTopicCatalogLst3) {
                                CatalogThree catalogThree = new CatalogThree();
                                catalogThree.idThree = catalog3.getId();
                                catalogThree.nameThree = catalog3.getName();
                                catalogThreeList.add(catalogThree);
                            }
                        }

                        catalogTwo.catalogThreeList = catalogThreeList;
                        catalogTwoList.add(catalogTwo);
                    }
                }

                model.catalogTwoList = catalogTwoList;

                returnLst.add(model);
            }
        }

        return returnLst;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        public String nameTwo;

        public String idTwo;

        // 三级目录ID
        public String idThree;

        // 三级目录名称
        public String nameThree;

        // 一级目录名称
        public String nameOne;

        // 一级目录Id
        public String idOne;

        public List<CatalogTwo> catalogTwoList;
    }

    public static class CatalogTwo {

        // 二级目录ID
        public String idTwo;

        // 二级目录名称
        public String nameTwo;

        public List<CatalogThree> catalogThreeList;
    }

    public static class CatalogThree {

        // 三级目录ID
        public String idThree;

        // 三级目录名称
        public String nameThree;
    }
}
